#include "Button.h"
#include "GameWorld.h"
#include "Defines.hpp"

Button::Button() : UIElement()
{
	x1 = 0;
	x2 = 0;
	y1 = 0;
	y2 = 0;
	text = "";
	m_name = "ignored_tile";
	m_mouseover_name = "ignored_tile";
	drawBox = false;
	

}

Button::~Button()
{
}

void Button::Update()
{
	int x, y;
	x = ClipPlane->GetX() + rect.x;
	y = ClipPlane->GetY() + rect.y;

	ChangeX(x);
	ChangeY(y);
}

void Button::DrawBox()
{
	if(drawBox)
	{
		/* R G B A */
		//rectangleRGBA(Entity::GetScreen(), x1, y1, x2 - 1, y2 - 1, 255, 0, 0, 255);
		//boxRGBA(Entity::GetScreen(), x1, y1, x2 - 1, y2 - 1, 255, 0, 0, 70);

		SDL_Rect rect;
		rect.x = Sint16(GetX());
		rect.y = Sint16(GetY());
		rect.w = TILE_DIMENSIONS;
		rect.h = TILE_DIMENSIONS;

		if(ClipPlane->IsInPlane(rect))
		{
			SDL_Rect aRect(rect);
			aRect.x = (aRect.x - ClipPlane->GetX());
			aRect.y = (aRect.y - ClipPlane->GetY());

			//boxRGBA(Entity::GetScreen(), aRect.x, aRect.y, aRect.x + aRect.w - 1, aRect.y + aRect.w - 1, 72, 61, 139, 120);

			OGL_Color aCol = {72 / 255.0f, 61 / 255.0f, 139 / 255.0f, 120/255.0f};
			DrawPrimitives->DrawRectangle(aRect.x, aRect.y, aRect.x + aRect.w, aRect.y + aRect.h, aCol);
			//boxRGBA(Entity::GetScreen(), aRect.x, aRect.y, aRect.x + aRect.w - 1, aRect.y + aRect.w - 1, 255, 0, 0, 70);
		}



	}
}


void Button::Load_Button(const char* filename, string name, string mouseover_name)
{

	/*m_name = name;
	m_mouseover_name = mouseover_name;

	ifstream myfile;
	myfile.open(filename);
	assert(myfile);

	if(myfile.is_open())
	{
		char* emptyStorage = (char*)malloc(100);

		SetImageID(name);

		myfile.getline(emptyStorage, 100, ':'); // Skip 'Coordinates' stop at the Coordinate information
		
		// Coordinates
		myfile >> x1; 
		myfile >> y1; 
		myfile >> x2;
		myfile >> y2;
		ChangeX(x1);
		ChangeY(y1);

		rect.x = x1;
		rect.y = y1;
		rect.w = x2 - x1;
		rect.h = y2 - y1;

		// Text
		myfile.getline(emptyStorage, 100, '=');	// Skip 'Text' and stop at the text information
		getline(myfile, text);

		// Close the file
		myfile.close();
		NullDeleteArray(emptyStorage); // cleaning up memory we allocated :)
	}*/
}

bool Button::inBounds( int x, int y )
{

    if( ( x >= x1 ) && ( x <= x2 ) && ( y >= y1 ) && ( y <= y2 ) )
	{
		SetImageID(m_mouseover_name);
		return true;
	}
    else
	{
		SetImageID(m_name);
        return false;
	}
}
	


